package aod.android.com.bernay.mine.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import aod.android.com.bernay.R;
import aod.android.com.bernay.mine.module.DynamicBean;

/**
 * @author crated by zhixianuanyang on 2019/4/9 15:44
 * @Email jamesKing0812@gmail.com
 */
public class DynamicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<DynamicBean> mVideoList;

    //定义三种常量  表示三种条目类型
    public static final int TYPE_IMAGE = 0;
    public static final int TYPE_VEDIO = 1;

    public DynamicAdapter(Context context, List<DynamicBean> videoList) {
        mContext = context;
        mVideoList = videoList;
    }

    @Override
    public int getItemCount() {
        return mVideoList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mVideoList.get(position).isAlbum()){
            return  TYPE_IMAGE;
        }else{
            return  TYPE_VEDIO;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        RecyclerView.ViewHolder holder = null;
        if (viewType == TYPE_IMAGE){
            View view = mInflater.inflate(R.layout.dynamic_list_image, parent, false);
            holder = new IamgeViewHolder(view,mContext);
        }else if(viewType == TYPE_VEDIO){
            View view = mInflater.inflate(R.layout.dynamic_list_vedio, parent, false);
            holder = new VideoViewHolder(view,mContext);
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof IamgeViewHolder ){
            DynamicBean dynamicBean = mVideoList.get(position);
            ((IamgeViewHolder) holder).bindData(dynamicBean);
        }else{
            DynamicBean dynamicBean = mVideoList.get(position);
            ((VideoViewHolder) holder).bindData(dynamicBean);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener !=null){
                    onItemClickListener.onItemClick(position);
                }
                if (onItemLongClickListener !=null){
                    onItemLongClickListener.onItemLongClick(position);
                }
            }
        });
    }

    private OnItemClickListener onItemClickListener;

    private OnItemLongClickListener onItemLongClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}